package com.bkq.cb;

import java.util.Calendar;

import com.bkq.cb.BLO.LoanBLO;
import com.bkq.cb.DTO.LoanDTO;

import android.R.integer;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LendScreen extends Activity{
	Button backBtn, saveBtn, cancelBtn;
	EditText namePersonTxt, dayFromTxt, dayToTxt, moneyTxt;
	LoanDTO loanObject = new LoanDTO();
	LoanBLO loanBlo = new LoanBLO(this);
	String idUser;
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.lendscreen);
	     
	     backBtn = (Button)findViewById(R.id.backIma_lendScreen);
	     saveBtn = (Button)findViewById(R.id.saveBnt_lendScreen);
	     cancelBtn =(Button)findViewById(R.id.cancelBnt_lendScreen);
	     namePersonTxt = (EditText)findViewById(R.id.borrowerTxt_lendScreen);
	     dayFromTxt = (EditText)findViewById(R.id.lendingdayTxt_lendScreen);
	     dayToTxt = (EditText)findViewById(R.id.returndayTxt_lendScreen);
	     moneyTxt = (EditText)findViewById(R.id.moneyTxt_lendScreen);
	     dayToTxt.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// TODO Auto-generated method stub
					 int y,m,d;
						Calendar c = Calendar.getInstance();        
				            y = c.get(Calendar.YEAR);        
				              m= c.get(Calendar.MONTH);        
				             d = c.get(Calendar.DAY_OF_MONTH); 
				             DatePickerDialog dp = new DatePickerDialog(LendScreen.this,new DatePickerDialog.OnDateSetListener() {
				            	   @Override
				                   public void onDateSet(DatePicker view, int year,
				                           int monthOfYear, int dayOfMonth) {
				                      String erg = null;
				                        erg = String.valueOf(dayOfMonth);                      
				                        erg += "/" + String.valueOf(monthOfYear+1);                    
				                        erg += "/" + year;  
				                    
				                       dayToTxt.setText(erg);
				                   
				                   }
				            	   
				                 }, y, m, d);
				             
				              dp.setTitle("Choose Date");
				              dp.show();
				}
			});
	     dayFromTxt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// TODO Auto-generated method stub
				 int y,m,d;
					Calendar c = Calendar.getInstance();        
			            y = c.get(Calendar.YEAR);        
			              m= c.get(Calendar.MONTH);        
			             d = c.get(Calendar.DAY_OF_MONTH); 
			             DatePickerDialog dp = new DatePickerDialog(LendScreen.this,new DatePickerDialog.OnDateSetListener() {
			            	   @Override
			                   public void onDateSet(DatePicker view, int year,
			                           int monthOfYear, int dayOfMonth) {
			                      String erg = null;
			                        erg = String.valueOf(dayOfMonth);                      
			                        erg += "/" + String.valueOf(monthOfYear+1);                    
			                        erg += "/" + year;  
			                    
			                       dayFromTxt.setText(erg);
			                   
			                   }
			            	   
			                 }, y, m, d);
			             
			              dp.setTitle("Choose Date");
			              dp.show();
			}
		});
	     Bundle extras = getIntent().getExtras();
	     if(extras!=null)
	    	 idUser = extras.getString("idUser").toString();
	     
	     backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
	     
	     saveBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				loanObject.IdUser = Integer.parseInt(idUser);
				loanObject.namePerson = namePersonTxt.getText().toString();
				loanObject.dayFrom = dayFromTxt.getText().toString();
				loanObject.dayTo = dayToTxt.getText().toString();
				loanObject.money = moneyTxt.getText().toString();
				loanObject.type = "Lend";
				if(loanBlo.insert(loanObject))
				{
					namePersonTxt.setText("");
					dayFromTxt.setText("");
					dayToTxt.setText("");
					moneyTxt.setText("");
					Toast.makeText(getBaseContext(), "insert success", Toast.LENGTH_SHORT).show();
				}
				else 
					Toast.makeText(getBaseContext(), "insert error", Toast.LENGTH_SHORT).show();
				
			}
		});
	     cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				
			}
		});
	     
	}
}
